/**
 * 触发按钮的基类
 * ibio-develop
 * 2008-11-24 15:14
 */
package com.ibio8.components.buttons {
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	/**
	 * 触发按钮的基类。
	 * 
	 * <p>
	 * 提供 8 帧包括选中/未选中状态的触发按钮。
	 * </p>
	 */
	public class BaseToggleButton extends MovieClip {
		/** @private **/
		protected var m_btn:MovieClip;
		/** @private **/
		//是否选中
		protected var m_selected:Boolean = false;
		/** @private **/
		//是否禁用
		protected var m_enabled:Boolean = true;
		/** @private **/
		protected var m_oldFrame:uint;
		/** @private **/
		protected var m_clickDoLater:Function;
		/** @private **/
		protected var m_clickDoLaterParams:Array;
		
		/**
		 * 构造函数
		 * @param	button	库里当前的按钮 MC 或者是舞台上还未添加到显示列表的 MC
		 */
		public function BaseToggleButton(button:MovieClip) {
			m_btn = button;
			m_btn.stop();
			m_btn.buttonMode = true;
			m_btn.mouseChildren = false;
			m_btn.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			m_btn.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			m_btn.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			m_btn.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			m_btn.addEventListener(Event.REMOVED_FROM_STAGE, onBtnRemovedHandler);
		}
		
		/**
		 * 如果是直接 new BaseToggleButton，则可通过 getter 返回当前的 button MC
		 */
		public function get self():MovieClip {
			return m_btn;
		}
		
		/**
		 * 点击后的回调函数
		 * @param	doLater	函数名称
		 * @param	...rest	该函数的参数
		 */
		public function onClick(doLater:Function, ...rest):void {
			m_clickDoLater = doLater;
			m_clickDoLaterParams = rest;
		}
		
		/**
		 * 是否为选中状态
		 */
		public function set selected(value:Boolean):void {
			m_selected = value;
			onBtnRollOut();
		}
		
		public function get selected():Boolean {
			return m_selected;
		}
		
		/**
		 * 是否禁用此按钮
		 */
		public override function set enabled(value:Boolean):void {
			if (m_enabled == value) {
				return;
			}
			m_enabled = value;
			//激活
			if (m_enabled) {
				m_btn.gotoAndStop(m_oldFrame);
				m_btn.mouseEnabled = true;
			}else {
				m_oldFrame = m_btn.currentFrame;
				//如果已经选中，则停在最后一帧
				if (m_selected) {
					m_btn.gotoAndStop(8);
				}else {
					m_btn.gotoAndStop(4);
				}
				m_btn.mouseEnabled = false;
			}
		}
		
		public override function get enabled():Boolean{
			return m_enabled;
		}
		
		/** @private **/
		protected function onRollOverHandler(e:MouseEvent):void {
			if(m_selected){
				m_btn.gotoAndStop(6);
			}else {
				m_btn.gotoAndStop(2);
			}
		}
		
		/** @private **/
		protected function onMouseDownHandler(e:MouseEvent):void {
			if(m_selected){
				m_btn.gotoAndStop(7);
			}else{
				m_btn.gotoAndStop(3);
			}
		}
		
		/** @private **/
		protected function onMouseUpHandler(e:MouseEvent):void {
			m_selected = !m_selected;
			onBtnRollOut();
			if (m_clickDoLater != null) {
				m_clickDoLater.apply(null, m_clickDoLaterParams);
			}
		}
		
		/** @private **/
		protected function onRollOutHandler(e:MouseEvent):void {
			onBtnRollOut();
		}
		
		/** @private **/
		protected function onBtnRollOut():void {
			if (m_enabled) {
				if(m_selected){
					m_btn.gotoAndStop(5);
				}else{
					m_btn.gotoAndStop(1);
				}
			}
		}
		
		/** @private **/
		protected function onBtnRemovedHandler(e:Event):void {
			/**
			 * 因为此时监听的为 REMOVED_FROM_STAGE，已经滤掉子类事件
			 * 所以这里去掉 if (e.target == m_btn) {
			 */
			m_btn.removeEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			m_btn.removeEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			m_btn.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			m_btn.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			m_btn.removeEventListener(Event.REMOVED_FROM_STAGE, onBtnRemovedHandler);
		}
	}
}